#!/bin/bash
#source env.sh  #only for test
function get_domid_by_name(){
if [ $# != 1 ]; then
    LOG_MSG  "$* parameter error";
    return 255
fi
local VMNAME=$1
local VMID=`xm domid $VMNAME`
if [ $? -eq 0 ];then
   echo $VMID   
   return 0
fi
return 255

#VMID=0
#xm list > tmp.$$
#while read vmname vmid other
#do 
#   LOG_MSG $vmname $vmid $other
#   if [ $vmname == $1 ];then
#       LOG_MSG "find"
#       VMID=$vmid
#       break; 
#   fi
#done <tmp.$$
#rm -rf tmp.$$
#return $VMID
}

function get_vname_by_id(){
if [ $# != 1 ]; then
    LOG_MSG  "$* parameter error";
    return 255
fi
local VMID=$1
local VMANME=`xm domname $VMID`
if [ $? -eq 0 ];then
   echo $VMNAME   
   return 0
fi
return 255
}


function create_vm_conf(){
local DEVICE_NAME=""
if [ $VM_OS_TYPE = "centos" ]; then
   DEVICE_NAME="xvda"
fi
if [ $VM_OS_TYPE = "minios" ]; then
   DEVICE_NAME="xvda1"
fi
if [ $# -lt 3 ]; then
    LOG_MSG  "$* parameter error";
    return 255
fi
local VMID=$1
local VMMEM=$2
if [ $# = 3 ];then
	vm_bridge[0]=$3
fi
if [ $# = 4 ];then
        vm_bridge[0]=$3
	vm_bridge[1]=$4
fi

local DIFFIMAGE_PATH=$VMS_WORK_DIR/VM-$VMID/sysdisk/diff.img
if [ $# = 3 ];then
cat <<EOF >$SYSDISK/vm-$VMID.cfg
name = "vm-$VMID"
memory = $VMMEM
vcpu = 1
bootloader="/usr/bin/pygrub"
disk = [ "file:$DIFFIMAGE_PATH,$DEVICE_NAME,w" ]
vif  = [ "bridge=${vm_bridge[0]}"]
root = "/dev/$DEVICE_NAME ro"
on_reboot   = 'restart'
on_crash    = 'restart'
EOF
fi
if [ $# = 4 ];then
cat <<EOF >$SYSDISK/vm-$VMID.cfg
name = "vm-$VMID"
memory = $VMMEM
vcpu = 1
bootloader="/usr/bin/pygrub"
disk = [ "file:$DIFFIMAGE_PATH,$DEVICE_NAME,w" ]
vif  = [ "bridge=${vm_bridge[0]}","bridge=${vm_bridge[1]}"]
root = "/dev/$DEVICE_NAME ro"
on_reboot   = 'restart'
on_crash    = 'restart'
EOF
fi
}
#get_domid_by_name Domain-0

